import 'dart:typed_data';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app/util/LogUtil.dart';
import 'package:flutter_app/util/ToastUtil.dart';
import 'package:flutter_app/view/dash_line.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:qrscan/qrscan.dart';

class CodeScanPage extends StatefulWidget {
  final String ?text;

  CodeScanPage({this.text}) : super();

  @override
  State<StatefulWidget> createState() {
    return CodeScanState();
  }
}

class CodeScanState extends State<CodeScanPage> {
  String codeScan = "";
  String creatCode = "李逵"; //二维码
  String bar = "AL0123456789"; //条形码
  static const platform = const MethodChannel("com.flutter.android.qrcode");

  //图片数据
  Uint8List bytes = Uint8List(0);
  Uint8List barCode = Uint8List(0);

  @override
  Widget build(BuildContext context) {

    return Scaffold(
        appBar: AppBar(
          title: Text(widget.text??""),
        ),
        body: SingleChildScrollView(
          padding:   EdgeInsets.all(30),
          child: Column(
            children: [
              Align(
                alignment: Alignment.topCenter,
                child: Center(
                  child: ElevatedButton(
                    child: Text("扫一扫"),
                    onPressed: scanQr,
                  ),
                ),
              ),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
                child: Text(
                  codeScan,
                  textAlign: TextAlign.center,
                ),
              ),
              SizedBox(
                height: 20,
              ),
              DashLine(),
              SizedBox(
                height: 20,
              ),
              Align(
                alignment: Alignment.topCenter,
                child: Center(
                  child: ElevatedButton(
                    child: Text("生成二维码"),
                    onPressed: _generateScanCode,
                  ),
                ),
              ),
              SizedBox(
                height: 20,
              ),
              Visibility(
                  visible: isVisible(bytes),
                  child: Container(
                    width: 200,
                    height: 200,
                    child: Image.memory(bytes),
                  )),
              SizedBox(
                height: 20,
              ),
              Align(
                alignment: Alignment.topCenter,
                child: Center(
                  child: ElevatedButton(
                    child: Text("生成条形码"),
                    onPressed: _generateBarCode,
                  ),
                ),
              ),
              SizedBox(
                height: 20,
              ),
              Visibility(
                  visible: isVisible(barCode),
                  child: Container(
                    width: 200,
                    height: 200,
                    child: Image.memory(barCode),
                  ))
            ],
          ),
        ));
  }

  ///扫描二维码
  scanQr() async {
    if (await Permission.camera.request().isGranted) {
      // 是否权限开启
      try {
        String? result = await scan(); //通过扫码获取二维码中的数据
        LogUtil.e("result " + result!);
        setState(() => this.codeScan = result);
      } catch (e) {
        if (e == CameraAccessDenied) {
          setState(() {
            this.codeScan = '没有拍照权限!';
          });
        } else {
          setState(() => this.codeScan = '未知错误: $e');
          print(e);
        }
      }
    } else {
      ToastUtil.toastShort("请求相机权限");
    }
  }

  ///生成二维码
  _generateScanCode() async {
    var uint8list = await generateBarCode(creatCode);
    this.setState(() {
      this.bytes = uint8list;
    });
  }

  ///生成条形码
  Future _generateBarCode() async {
    if (bar != null && bar.isNotEmpty) {
      var code = await platform.invokeMethod("generate_barcode", {'code': bar});
      setState(() {
        barCode = code;
      });
    }
  }

  ///是否显示UI
  isVisible(Uint8List bytes) {
    var length = bytes.length;
    return length > 0;
  }
}
